package com.liyunc.demo.comp.json.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.liyunc.demo.comp.json.converter.CustomMappingJackson2HttpMessageConverter;
import com.liyunc.demo.comp.json.mapper.CustomObjectMapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.MediaType;

import java.util.Collections;


/**
 * @author liyuncong
 * @version 1.0
 * @file CustomJsonConfiguration
 * @brief CustomJsonConfiguration
 * @details CustomJsonConfiguration
 * @date 2022/5/21
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                        NAME               DESCRIPTION
 * 2022/5/21                     liyuncong          Created
 */
@Log4j2
@Configuration
@ComponentScan(basePackages = "com.liyunc.demo.comp.json")
public class CustomJsonConfiguration {

    public static final PropertyNamingStrategy propertyNamingStrategy =
        PropertyNamingStrategies.LOWER_CAMEL_CASE;
    private final CustomObjectMapper customObjectMapper =
        new CustomObjectMapper(propertyNamingStrategy);
    private final CustomMappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter =
        new CustomMappingJackson2HttpMessageConverter(customObjectMapper);


    @Primary
    @Bean
    public ObjectMapper customObjectMapper() {
        log.debug("instance custom objectMapper");
        return customObjectMapper;
    }

    @Bean
    public CustomMappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        log.debug("instance custom mappingJackson2HttpMessageConverter");
        mappingJackson2HttpMessageConverter
            .setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
        return mappingJackson2HttpMessageConverter;
    }
}